clear;clc;

Folder      =   'TableGraphs\\DifferentCorr\\';
mkdir(Folder);

load('Results_DifferentCorr.mat');

%% Collect the Parameter Values
Num_Fin     =   length(SOLUTION_Fin);
Num_Real    =   length(SOLUTION_Real);

XAxis_Fin   =   zeros(Num_Fin,1);
XAxis_Real  =   zeros(Num_Real,1);

for ii=1:Num_Fin
    XAxis_Fin(ii)   =   ( log(SOLUTION_Fin{ii}.SS.Agg.Avg.f.ext)- ...
                          log(SOLUTION_Fin{ii}.SS.Agg.Avg.f.dom) )*100;
end

for ii=1:Num_Real
    XAxis_Real(ii)  =   ( log(SOLUTION_Real{ii}.SS.Agg.Avg.f.H)- ...
                          log(SOLUTION_Real{ii}.SS.Agg.Avg.f.N) )*100;
end

%% Collect the Moments
Mom_Fin     =   SubFun_ResultCell2Mom(SOLUTION_Fin);
Mom_Real    =   SubFun_ResultCell2Mom(SOLUTION_Real);

%% Figures
List_Shock  =   fieldnames(Mom_Fin.Stat);
N_col       =   1;
N_row       =   1;
fig_size    =   [N_col,N_row].*[1,0.75]*1/2.5; 
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.02,0.02];
fig_HMargin =   [0.15,0.02];
%--------------------------------------------------------------------------
% Aggregate
%--------------------------------------------------------------------------
fig_SubPlot =   @(ii_vv)subtightplot(N_row,N_col,ii_vv,...
                                     fig_gap,fig_VMargin,fig_HMargin);

XLabel      =   @(Label)xlabel(Label,'fontsize',8,'interpreter','latex');
YLabel      =   @(Label)ylabel(Label,'fontsize',8,'interpreter','latex');


AxisSetup   =   GR_Axis('TickFmt',struct('X','%g','Y','%.2g'),...
                       'FontSize',struct('X',8,'Y',8),...
                       'Exponent',struct('X',0,'Y',0),...
                       'Box','off');

Tick_X      =   @(X)GR_TickValue('Unit',25).Setup(X);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0,'MinUnit',0.01).Setup(Y);

LabelList   =   {'Consumption','Inflation','Exchange Rate'};
Line_List   =   {GR_Line('Style','-','Color',MyColor('Black'),'Width',3);...
                 GR_Line('Style','--','Color',MyColor('Blue'),'Width',2);...
                 GR_Line('Style',':','Color',MyColor('Red'),'Width',2) ...
                 };
             
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',6,...
                                    'interpreter','latex','location','southoutside',...
                                    'NumColumns',2);
TempXLabel  =   'Log Avg. Net Wealth Difference, Int. r.t. Nonint. (\%)';
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};
    
    % by Fin
    Fig         =   FigureSetup(['Aggregate, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(1:3,:);
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(TempXLabel);
    YLabel('Peak Responses (pp)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'Agg_byFI_',SH]);
    close(Fig);
    
    % by Real
    Fig         =   FigureSetup(['Aggregate, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(1:3,:);
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(TempXLabel);
    YLabel('Peak Responses (pp)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'Agg_byRI_',SH]);
    close(Fig);
end

%--------------------------------------------------------------------------
% Variance Decomposition
%--------------------------------------------------------------------------
AxisSetup   =   GR_Axis('TickFmt',struct('X','%g','Y','%.2g'),...
                       'FontSize',struct('X',8,'Y',8),...
                       'Exponent',struct('X',0,'Y',0),...
                       'Box','off');

Tick_X      =   @(X)GR_TickValue('Unit',25).Setup(X);
Tick_Y      =   @(Y)GR_TickValue('Symmetric',0,'MinUnit',0.01).Setup(Y);

LabelList   =   {'Real Integration','Financial Integration','Net Wealth'};
Line_List   =   {GR_Line('Style','-','Color',MyColor('Black'),'Width',2);...
                 GR_Line('Style','-','Color',MyColor('Gray'),'Width',2);...
                 GR_Line('Style','--','Color',MyColor('Blue'),'Width',2) ...
                 };
             
LEG         =   @(PH,LabelList)legend(PH,LabelList,'fontsize',6,...
                                    'interpreter','latex','location','southoutside',...
                                    'NumColumns',2);
TempXLabel  =   'Log Avg. Net Wealth Difference, Int. r.t. Nonint. (\%)';
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};
    
    % by Fin
    Fig         =   FigureSetup(['Aggregate, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(8:10,:);
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(TempXLabel);
    YLabel('Relevance of Cross-group Variation (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'VarDec_byFI_',SH]);
    close(Fig);
    
    % by Real
    Fig         =   FigureSetup(['Aggregate, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(8:10,:);
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(TempXLabel);
    YLabel('Relevance of Cross-group Variation (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'VarDec_byRI_',SH]);
    close(Fig);
end

%--------------------------------------------------------------------------
% Distributional
%--------------------------------------------------------------------------
LabelList   =   {'Std.','Gap by R.I.','Gap by F.I.','Gap by N.W.'};
Line_List   =   {GR_Line('Style','-','Color',MyColor('Black',1),'Width',3);...
                 GR_Line('Style','--','Color',MyColor('Black',0.6),'Width',2);...
                 GR_Line('Style','-.','Color',MyColor('Black',0.4),'Width',2); ...
                 GR_Line('Style',':','Color',MyColor('Black',0.2),'Width',2) ...
                 };
for ii_Sh=1:length(List_Shock)
    SH          =   List_Shock{ii_Sh};
    
    %=======
    % by Fin
    %=======
    % Non-Normalized
    Fig         =   FigureSetup(['Dispersion, Non-normalized, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(4:7,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(TempXLabel);
    YLabel('Consumption Response Dispersion (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_NonNorm_byFI_',SH]);
    close(Fig);
    % Normalized
    Fig         =   FigureSetup(['Dispersion, Normalized, By Fin, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Fin.Stat.(SH)(4:7,:)./Mom_Fin.Stat.(SH)(1,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Fin',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Fin),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Fin),max(XAxis_Fin)];
    XLabel(TempXLabel);
    YLabel('Consumption Response Dispersion');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_Norm_byFI_',SH]);
    close(Fig);
    %========
    % by Real
    %========
    % Non-Normalized
    Fig         =   FigureSetup(['Dispersion, Non-normalized, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(4:7,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(TempXLabel);
    YLabel('Consumption Response Dispersion (\%)');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_NonNorm_byRI_',SH]);
    close(Fig);
    % Normalized
    Fig         =   FigureSetup(['Dispersion, Normalized, By Real, ',SH],fig_size);
    ax          =   fig_SubPlot(1);
    TempMom     =   Mom_Real.Stat.(SH)(4:7,:)./Mom_Real.Stat.(SH)(1,:);
    
    [PH,ax]     =   Plot_Line(XAxis_Real',TempMom,...
                              'ax',ax,'LineProperty',Line_List,...
                              'XTick',Tick_X(XAxis_Real),'YTick',Tick_Y(TempMom));
    ax.XLim     =   [min(XAxis_Real),max(XAxis_Real)];
    XLabel(TempXLabel);
    YLabel('Consumption Response Dispersion');
    LEG(PH,LabelList);
    AxisSetup.Setup(ax);
    
    print('-depsc','-r1000',Fig,[Folder,'ConsDisp_Norm_byRI_',SH]);
    close(Fig);
end

